package gui.panel_new.evaluation.professor;

import enums.*;
import gui.other.SylogismViewerDialog;
import gui.panel_new.BasicVennPanel;
import gui.panel_new.evaluation.exeptions.ExerciseNotBuiltException;
import gui.panel_new.evaluation.exeptions.PunctuationZeroException;
import j_util.Utiles;
import misc.BundlesProvider;
import model.Polisilogismo;
import model.Premisa;
import model.Sylogism;
import model.Venn;
import model.eval.TestExcercise;
import model.eval.TestExerciseVenn;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Extiende de CreateExercisePanel, permite crear ejercicios en los que se analizan los silogismos a partir
 * de los diagramas de Venn;
 *
 * @author Jose Martin Rozanec;
 */
public class CreateVennPanel extends CreateExercisePanel {
    private javax.swing.JButton analizarButton;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JComboBox combo1;
    private javax.swing.JComboBox combo2;
    private javax.swing.JComboBox combo3;
    private javax.swing.JButton completarButton;
    private javax.swing.JTextField conclusionField;
    private javax.swing.JLabel conclusionLabel;
    private javax.swing.JLabel convertirEnLabel;
    private javax.swing.JPanel cuadroPanel;
    private javax.swing.JLabel figuraLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTextField premisaMayorField;
    private javax.swing.JLabel premisaMayorLabel;
    private javax.swing.JTextField premisaMenorField;
    private javax.swing.JLabel premisaMenorLabel;
    private javax.swing.JButton resetearButton;
    private javax.swing.JLabel terminoMayorLabel;
    private javax.swing.JLabel terminoMedioLabel;
    private javax.swing.JLabel terminoMenorLabel;
    private javax.swing.JTextArea textArea;
    private JButton premisaMayorSugestion;
    private JButton premisaMenorSugestion;
    private JButton conclusionSugestion;
    private BasicVennPanel solutionVennPanel;

    private JLabel addsPoints;
    private JLabel substractPoints;
    private JTextField addsPointsField;
    private JTextField substractPointsField;

    private int premisaLabelsStringLength;
    private ResourceBundle textBundle;

    private Sylogism sylogism;

    /**
     * @param saveButton  - saveButton;
     * @param openButton  - openButton;
     * @param fileChooser - fileChooser;
     */
    public CreateVennPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        fileFilter = FileFilterEnum.XML;
        testPanelEnum = TestPanelEnum.VENN;
        this.sylogism = new Sylogism();
        premisaLabelsStringLength = 10;
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        initComponents();
        completarButton.setEnabled(false);
        reset();
    }

    /**
     * Inicializa componentes;
     */
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        cuadroPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        premisaMayorField = new javax.swing.JTextField();
        premisaMayorLabel = new javax.swing.JLabel();
        convertirEnLabel = new javax.swing.JLabel();
        combo1 = new javax.swing.JComboBox();
        premisaMenorLabel = new javax.swing.JLabel();
        premisaMenorField = new javax.swing.JTextField();
        combo2 = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        conclusionLabel = new javax.swing.JLabel();
        conclusionField = new javax.swing.JTextField();
        combo3 = new javax.swing.JComboBox();
        completarButton = new javax.swing.JButton();
        analizarButton = new javax.swing.JButton();
        resetearButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        figuraLabel = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        terminoMayorLabel = new javax.swing.JLabel();
        terminoMedioLabel = new javax.swing.JLabel();
        terminoMenorLabel = new javax.swing.JLabel();
        bottomPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        solutionVennPanel = new BasicVennPanel();

        addsPoints = new JLabel(textBundle.getString(TextBundleEnum.SUMA.toString()));
        substractPoints = new JLabel(textBundle.getString(TextBundleEnum.RESTA.toString()));
        addsPointsField = new JTextField(7);
        addsPointsField.setEditable(false);
        substractPointsField = new JTextField(7);
        substractPointsField.setEditable(false);

        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        cuadroPanel.setBackground(new java.awt.Color(255, 255, 255));
        cuadroPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        premisaMayorField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                if (Premisa.isComplete(premisaMayorField.getText())) {
                    sylogism.setPremisa(new Premisa(premisaMayorField.getText()), PremisaTypeEnum.FIRST);
                    premisaMayorField.setForeground(new Color(0, 0, 0));
                    setPropertiesOrSuggestions();
                    premisaMayorLabel.setText(
                            Utiles.getFormattedString(textBundle.getString(
                                    TextBundleEnum.PREMISA_MAYOR.toString()), "  -  " +
                                    textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                                    sylogism.getPremisa(PremisaTypeEnum.FIRST).getTermsExtensionString()));
                    premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_BREVE)));
//                    combo1.setSelectedIndex(0);
//                    enableSuggestionButtons();
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.FIRST);
                    premisaMayorField.setForeground(new Color(255, 0, 0));
                    setPropertiesOrSuggestions();
                    premisaMayorLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()), ""));
                    premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));
//                    enableSuggestionButtons();
                }
            }
        });

        premisaMenorField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                if (Premisa.isComplete(premisaMenorField.getText())) {
                    sylogism.setPremisa(new Premisa(premisaMenorField.getText()), PremisaTypeEnum.SECOND);
                    premisaMenorField.setForeground(new Color(0, 0, 0));
                    setPropertiesOrSuggestions();
                    premisaMenorLabel.setText(Utiles.getFormattedString(textBundle.getString(
                            TextBundleEnum.PREMISA_MENOR.toString()), "  -  " +
                            textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                            sylogism.getPremisa(PremisaTypeEnum.SECOND).getTermsExtensionString()));
                    premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_BREVE)));
//                    combo2.setSelectedIndex(0);
//                    enableSuggestionButtons();
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.SECOND);
                    premisaMenorField.setForeground(new Color(255, 0, 0));
                    setPropertiesOrSuggestions();
                    premisaMenorLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()), ""));
                    premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));
//                    enableSuggestionButtons();
                }
            }
        });

        conclusionField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                if (Premisa.isComplete(conclusionField.getText())) {
                    sylogism.setPremisa(new Premisa(conclusionField.getText()), PremisaTypeEnum.CONCLUSION);
                    conclusionField.setForeground(new Color(0, 0, 0));
                    setPropertiesOrSuggestions();
                    conclusionLabel.setText(Utiles.getFormattedString(textBundle.getString(
                            TextBundleEnum.CONCLUSION.toString()), "  -  " +
                            textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                            sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).getTermsExtensionString()));
                    conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_BREVE)));
//                    combo3.setSelectedIndex(0);
//                    setComboValue(combo3,sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).getPartsExtension());
//                    enableSuggestionButtons();
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.CONCLUSION);
                    conclusionField.setForeground(new Color(255, 0, 0));
                    setPropertiesOrSuggestions();
                    conclusionLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.CONCLUSION.toString()), ""));
                    conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_LARGO)));
//                    enableSuggestionButtons();
                }
            }
        });

        premisaMayorLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()), ""));
        premisaMayorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        convertirEnLabel.setText(textBundle.getString(TextBundleEnum.CONVERTIR_EN.toString()));

        combo1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"-", "A", "E", "I", "O"}));
        combo1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.FIRST, combo1.getSelectedIndex());
            }
        });

        premisaMenorLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()), ""));
        premisaMenorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        combo2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"-", "A", "E", "I", "O"}));
        combo2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.SECOND, combo2.getSelectedIndex());
            }
        });

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        conclusionLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.CONCLUSION.toString()), ""));
        conclusionLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_LARGO)));

        combo3.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"-", "A", "E", "I", "O"}));
        combo3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.CONCLUSION, combo3.getSelectedIndex());
            }
        });

        completarButton.setText(textBundle.getString(TextBundleEnum.COMPLETAR.toString()));
        completarButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sylogism.complete();
                setSylogism(sylogism);
            }
        });
        completarButton.setMnemonic(KeyEvent.VK_C);

        analizarButton.setText(textBundle.getString(TextBundleEnum.ANALIZAR.toString()));
        analizarButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                analize();
            }
        });
        analizarButton.setMnemonic(KeyEvent.VK_A);

        resetearButton.setText(textBundle.getString(TextBundleEnum.RESET.toString()));
        resetearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        resetearButton.setMnemonic(KeyEvent.VK_R);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.FIGURA.toString())));

        figuraLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_NONE)));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true), textBundle.getString(TextBundleEnum.TERMINOS.toString())));

        terminoMayorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()) + ":");

        terminoMedioLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()) + ":");

        terminoMenorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()) + ":");

//        bottomPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Sugerencias y Analisis"), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)));
        bottomPanel.setBorder(BorderFactory.createTitledBorder((Border)javax.swing.BorderFactory.createMatteBorder(1,0,0,0,new java.awt.Color(204, 204, 204)),
                textBundle.getString(TextBundleEnum.SUGERENCIAS_ANALISIS.toString())));

        jScrollPane1.setViewportView(textArea);

        bottomPanel.getAccessibleContext().setAccessibleName(textBundle.getString(TextBundleEnum.ANALISIS.toString()));

        sizeTextFields();
        sizeTextAreas();
        layoutComponents();
    }

    /**
     * Realiza el layout de los componentes visuales;
     */
    private void layoutComponents() {
        setBackground(Color.WHITE);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int x = 0;
        int y = 0;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        jPanel1.setLayout(new GridBagLayout());

        /*      PREMISAS     */
        jPanel1.add(premisaMayorLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x += 7);
        jPanel1.add(convertirEnLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(premisaMayorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);

        premisaMayorSugestion = new JButton(
                new PremisaSuggestionAction(PremisaTypeEnum.SECOND, PremisaTypeEnum.CONCLUSION, premisaMayorField));
        premisaMayorSugestion.setToolTipText(textBundle.getString(TextBundleEnum.SUGERENCIA.toString()) + "...");

        jPanel1.add(premisaMayorSugestion, gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo1, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorLabel, gridBagConstraints);

        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        premisaMenorSugestion = new JButton(
                new PremisaSuggestionAction(PremisaTypeEnum.FIRST, PremisaTypeEnum.CONCLUSION, premisaMenorField));
        premisaMayorSugestion.setToolTipText(textBundle.getString(TextBundleEnum.SUGERENCIA.toString()) + "...");
        jPanel1.add(premisaMenorSugestion, gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo2, gridBagConstraints);

        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        jSeparator1.setPreferredSize(new Dimension(550, 2));
        jPanel1.add(jSeparator1, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(conclusionLabel, gridBagConstraints);

        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(conclusionField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        conclusionSugestion = new JButton(
                new PremisaSuggestionAction(PremisaTypeEnum.FIRST, PremisaTypeEnum.SECOND, conclusionField));
        premisaMayorSugestion.setToolTipText(textBundle.getString(TextBundleEnum.SUGERENCIA.toString()) + "...");
        jPanel1.add(conclusionSugestion, gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo3, gridBagConstraints);

        jPanel1.setMinimumSize(new Dimension(655, 225));
        jPanel1.setPreferredSize(new Dimension(655, 225));
        x = 0;
        y = 0;
        /*      END OF PREMISAS     */

        /*      BUTTONS     */
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        add(jPanel1, gridBagConstraints);

        JPanel buttonsPanel = new JPanel(new GridLayout(1, 7));
        buttonsPanel.setBackground(Color.WHITE);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(completarButton);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(analizarButton);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(resetearButton);
        buttonsPanel.add(new JLabel());

        gridBagConstraints.gridy = 1;
        add(buttonsPanel, gridBagConstraints);
        /*      END OF BUTTONS     */

        jPanel2.setMinimumSize(new Dimension(215, 175));
        jPanel2.setPreferredSize(new Dimension(215, 175));
        jPanel2.add(figuraLabel);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;

        jPanel3 = new JPanel();
        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.DIAGRAMA_SOLUCION.toString())));
        jPanel3.add(solutionVennPanel);
        solutionVennPanel.setMinimumSize(new Dimension(230, 230));
        solutionVennPanel.setPreferredSize(new Dimension(230, 230));

//        add(createFigurePanel(), gridBagConstraints);

        JPanel centerPanel = new JPanel(new GridLayout(1,2));
        centerPanel.add(createFigurePanel());
        centerPanel.add(jPanel3);
        gridBagConstraints.insets = new Insets(5,65,0,60);
        add(centerPanel,gridBagConstraints);
        gridBagConstraints.insets = new Insets(0,5,0,5);

//        gridBagConstraints.gridwidth = 1;
//        gridBagConstraints.gridx = 1;
//        gridBagConstraints.gridy = 2;
//        add(jPanel3, gridBagConstraints);

        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;

        bottomPanel.setBackground(Color.WHITE);
        bottomPanel.setMinimumSize(new Dimension(655, 100));
        bottomPanel.setPreferredSize(new Dimension(655, 100));
        bottomPanel.add(jScrollPane1);
        add(bottomPanel, gridBagConstraints);        
    }

    private JPanel createFigurePanel(){
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gri = new GridBagConstraints();
        gri.gridheight=2;
        panel.add(jPanel2,gri);
        gri.gridy=2;
        panel.add(createScoringPanel(),gri);
        panel.setBackground(Color.WHITE);
        panel.setMinimumSize(new Dimension(230, 255));
        panel.setPreferredSize(new Dimension(230, 255));
        return panel;
    }

    private JPanel createScoringPanel() {
        JPanel scoringPanel = new JPanel();
        scoringPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1,0,0,1,Color.LIGHT_GRAY),
                textBundle.getString(TextBundleEnum.PUNTAJE.toString())));
        scoringPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(2, 5, 2, 5);
        scoringPanel.add(addsPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(addsPointsField, gridBagConstraints);
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        scoringPanel.add(substractPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(substractPointsField, gridBagConstraints);
        scoringPanel.setBackground(Color.WHITE);
        return scoringPanel;
    }

    /**
     * Determina el tamano de los textFields;
     */
    private void sizeTextFields() {
        premisaMayorField.setColumns(45);
        premisaMayorField.setMinimumSize(new Dimension(550, 20));
        premisaMenorField.setColumns(45);
        premisaMenorField.setMinimumSize(new Dimension(550, 20));
        conclusionField.setColumns(45);
        conclusionField.setMinimumSize(new Dimension(550, 20));
    }

    /**
     * Determina el tamano de los textAreas;
     */
    private void sizeTextAreas() {
        textArea.setColumns(58);
        textArea.setEditable(false);
        textArea.setRows(7);
        textArea.setPreferredSize(new Dimension(550,110));
    }

    /**
     * Aministra las sugerencias y determina la figura del silogismo;
     */
    private void setPropertiesOrSuggestions() {
        switch (sylogism.getFigure()) {
            case FIRST:
                figuraLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_1)));
                break;
            case SECOND:
                figuraLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_2)));
                break;
            case THIRD:
                figuraLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_3)));
                break;
            case FOURTH:
                figuraLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_4)));
                break;
            default:
                figuraLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.FIGURA_NONE)));
                break;
        }
        textArea.setText(sylogism.getPremisaSuggestion());
        terminosSilogismoEnVennManager();
        manageCompleteButton();
    }

    private void terminosSilogismoEnVennManager(){
        if(sylogism.getTerminoMayor().equals("")){
            solutionVennPanel.setTerminoMayor(textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()));
        }else{
            solutionVennPanel.setTerminoMayor(sylogism.getTerminoMayor());
        }
        if(sylogism.getTerminoMedio().equals("")){
            solutionVennPanel.setTerminoMedio(textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()));
        }else{
            solutionVennPanel.setTerminoMedio(sylogism.getTerminoMedio());
        }
        if(sylogism.getTerminoMenor().equals("")){
            solutionVennPanel.setTerminoMenor(textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()));
        }else{
            solutionVennPanel.setTerminoMenor(sylogism.getTerminoMenor());
        }
        solutionVennPanel.setVennEnums(Venn.analizeSylogism(sylogism));
    }

    /**
     * Analiza el silogismo;
     */
    private void analize() {
        textArea.setText(sylogism.analize());
        solutionVennPanel.setVennEnums(Venn.analizeSylogism(sylogism));
    }

    /**
     * Resetea el silogismo y toda la parte grafica asociada;
     */
    public void reset() {
        sylogism = new Sylogism();
        premisaMayorField.setText("");
        premisaMenorField.setText("");
        conclusionField.setText("");
        setPropertiesOrSuggestions();
        addsPointsField.setText("10");
        addsPointsField.setEditable(true);
        substractPointsField.setText("5");
        substractPointsField.setEditable(true);
    }

    /**
     * Administra la conversion de la premisa a una extension seleccionada;
     *
     * @param premisaTypeEnum;
     * @param selectedIndex    - indice que refiere al comboBox de seleccion de extension de la premisa;
     */
    public void comboConvertOrComplete(PremisaTypeEnum premisaTypeEnum, int selectedIndex) {
        if (sylogism.getPremisa(premisaTypeEnum) != null) {
            switch (selectedIndex) {
                case 1://A
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.A);
                    break;
                case 2://E
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.E);
                    break;
                case 3://I
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.I);
                    break;
                case 4://O
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.O);
                    break;
                case 0://no hace nada
                    break;
            }
            switch (premisaTypeEnum) {
                case FIRST:
                    premisaMayorField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
                case SECOND:
                    premisaMenorField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
                case CONCLUSION:
                    conclusionField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
            }
        }
        setPropertiesOrSuggestions();
    }

    /**
     * Permite setear el silogismo;
     *
     * @param sylogism
     */
    private void setSylogism(Sylogism sylogism) {
        System.out.println("Setting sylogism...");//todo delete
        this.sylogism = sylogism;
        if (sylogism.getPremisa(PremisaTypeEnum.FIRST) != null) {
            premisaMayorField.setText(sylogism.getPremisa(PremisaTypeEnum.FIRST).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.SECOND) != null) {
            premisaMenorField.setText(sylogism.getPremisa(PremisaTypeEnum.SECOND).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.CONCLUSION) != null) {
            conclusionField.setText(sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).toString());
        }
        setPropertiesOrSuggestions();
        analize();
    }

    /**
     * Maneja el boton "Completar", habilitandolo o desabilitandolo segun corresponda;
     */
    private void manageCompleteButton() {
        int i = 0;
        if (!sylogism.getTerminoMayor().equals("")) {
            i++;
        }
        if (!sylogism.getTerminoMedio().equals("")) {
            i++;
        }
        if (!sylogism.getTerminoMenor().equals("")) {
            i++;
        }
        if (i >= 2) {
            completarButton.setEnabled(true);
        } else {
            completarButton.setEnabled(false);
        }
    }

    protected void setSaveActionListener() {
        class SaveSylogismAction extends AbstractAction {
            public SaveSylogismAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
                if (!sylogism.isComplete()) {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                            textBundle.getString(TextBundleEnum.WARNING_NO_GUARDAR_SILOGISMO_VACIO.toString()),
                            "", JOptionPane.WARNING_MESSAGE);
                } else {
                    String path = "";
                    int state = 0;
                    fileChooser.showSaveDialog(JOptionPane.getRootFrame());
                    if (state == JFileChooser.APPROVE_OPTION) {
                        path = fileChooser.getSelectedFile().getPath();
                        Utiles.saveTextFileAlertingOverwrite(path, ".xml", sylogism.toXML());
                    }
                }
            }
        }
        saveButton.setAction(new SaveSylogismAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(true);
    }

    protected void setOpenActionListener() {
        class OpenSylogismAction extends AbstractAction {
            public OpenSylogismAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPEN)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = 0;
                fileChooser.showOpenDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = "";
                    fileChooser.getSelectedFile().getPath();
                    try {
                        RandomAccessFile randomAccessFile = new RandomAccessFile(path, "r");
                        String xml = randomAccessFile.readUTF();

                        if (xml.startsWith(Sylogism.openTag())) {
                            setSylogism(Sylogism.parseXML(xml));
                        } else {
                            if (xml.startsWith(Polisilogismo.openTag())) {
                                SylogismViewerDialog sylogismViewerDialog = new SylogismViewerDialog(
                                        Polisilogismo.parseXML(xml).getIterator());
                                Utiles.centerDialog(sylogismViewerDialog);
                                sylogismViewerDialog.setSize(337, 160);
                                sylogismViewerDialog.setResizable(false);
                                sylogismViewerDialog.setModal(true);
                                sylogismViewerDialog.setVisible(true);
                                if (sylogismViewerDialog.accepted()) {
                                    setSylogism(sylogismViewerDialog.getSylogism());
                                }
                            } else {
                                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                                        textBundle.getString(TextBundleEnum.WARNING_ARCHIVO_INCORRECTO_SILOGISMO.toString()));
                            }
                        }
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        openButton.setAction(new OpenSylogismAction());
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(true);
    }

    protected void setFocusOnComponent() {
        //remains unimplemented...
    }

    class PremisaSuggestionAction extends AbstractAction {
        private PremisaTypeEnum first;
        private PremisaTypeEnum second;
        private JTextField newPremisaField;

        public PremisaSuggestionAction(PremisaTypeEnum first, PremisaTypeEnum second, JTextField newPremisaField) {
            super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.LIGHT_BULB)));
            this.first = first;
            this.second = second;
            this.newPremisaField = newPremisaField;
        }

        public void actionPerformed(ActionEvent e) {
            Premisa firstPremisa = sylogism.getPremisa(first);
            Premisa secondPremisa = sylogism.getPremisa(second);

            if (firstPremisa != null && secondPremisa != null) {
                if (Utiles.getRandomInt(2) == 1) {
                    newPremisaField.setText(Premisa.getRandomPremisa(firstPremisa).toString());
                } else {
                    newPremisaField.setText(Premisa.getRandomPremisa(secondPremisa).toString());
                }
            } else {
                if (firstPremisa != null) {
                    newPremisaField.setText(Premisa.getRandomPremisa(firstPremisa).toString());
                } else {
                    if (secondPremisa != null) {
                        newPremisaField.setText(Premisa.getRandomPremisa(secondPremisa).toString());
                    } else {
                        newPremisaField.setText(Premisa.getRandomPremisa().toString());
                    }
                }
            }
        }
    }

    private void enableSuggestionButtons() {
        if (sylogism.isComplete()) {
            premisaMayorSugestion.setEnabled(false);
            premisaMenorSugestion.setEnabled(false);
            conclusionSugestion.setEnabled(false);
        } else {
            premisaMayorSugestion.setEnabled(true);
            premisaMenorSugestion.setEnabled(true);
            conclusionSugestion.setEnabled(true);
        }
    }

    public TestExcercise getExercise() throws ExerciseNotBuiltException, PunctuationZeroException {
        if (!sylogism.isComplete() || addsPointsField.getText().equals("") ||
                substractPointsField.getText().equals("")) {
            throw new ExerciseNotBuiltException();
        }
        if (Integer.parseInt(addsPointsField.getText()) == 0) {
            throw new PunctuationZeroException();
        }
        TestExcercise testExercise = new TestExerciseVenn();

        testExercise.setExerciseData(sylogism);
        testExercise.setStudentsSolution(Venn.getDefaultVennRepresentation());
        testExercise.setCorrectScore(Integer.parseInt(addsPointsField.getText()));
        if (substractPointsField.getText().equals("")) {
            substractPointsField.setText("0");
        }
        testExercise.setIncorrectScore(Integer.parseInt(substractPointsField.getText()));
        return testExercise;
    }

    public void setExercise(TestExcercise testExercise) {
        setSylogism((Sylogism) testExercise.getExcerciseData());
        addsPointsField.setText("" + testExercise.getCorrectScore());
        substractPointsField.setText("" + testExercise.getIncorrectScore());
    }
}
